package com.skd.mall.order.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.skd.mall.order.vo.PayVo;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
@Component
public class AlipayTemplate {

    // 商户appid 沙箱账号: acftsx4412@sandbox.com
    // 密码 111111
    public static String APPID = "9021000140684297";
    // 私钥 pkcs8格式的
    public static String RSA_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIjUnupDrXCyByDVqEcoafAKIOrvMTyRz46xS+NhtoVO4cRPAxW9Gf9pwtzGhal3/NzIMHfV9d2uzLlRHzfP/LBCkiLRfJgixSyIjZTlZGILgiGEYndZVHHChpU1IMORJwNkksGnD07WF9nWaqMO31WnQCm0Ayd7OUT1AGzxYeKvQ33cFWeMdfSHeJwi+QM20gic9gp1pxkwrNFltB2/8Bt567AJXblSEGTe08OeeFhfPsRrg3n+7yK8l2TRas2g4Qgl5zPkN0RaO3jKNriKGs+rWvimXWTDriJWuUNg1+FeP7cCsT8VBjLDCM3auAglCMTe3bHZNz/KeXeHU8y99ZAgMBAAECggEAchuHNXldhgfRRB1uj5FYd5RC1/xQOi9sG4HuYoi6XHPkShE9mjF9OawNlKPkbBd/gA0vTVnyEG76Q6E9T/gblJ3Uda+y2fwCZ+cVnGm+5HlBBZS9QwoaBUM9bc5nmLk0dDqY6Nbrn3CicCLRANVEdCvzF55+he3LTdr9J8NzWx8LZNZ2x34NcATFd9ykWmHCjFOKrj4i8NAHwWU6vzz5NOxOAUbNv8B+8RLEzusUdwBna1madIRHEa3GnOuZ/OeM25J0xG5GbchWVhfvDGwqV+jyMUJBnSjNTZHEJ8BQFmdGFDLecQX/KnPVDD161JzSSOchZTirzBKMzUFDRninpQKBgQD2XrgbGy4EWNjhBDzsqnQjystW9juL0KiUOW1Bp0HILfDZDCv5PwiwJvERXxHdFKhgJu5Hudnd2247Yub5Ig6tamZs3Ub9rLjKUD/v/Ofdbw1vc27RdDoyjgEmiSOUnzYa+tndeIDcPEU/Ypwi9Yrk5SWG5AVzeL02MPu6A3XFnwKBgQCN468g87KppGVv9C4Oot9+hW2pdQOyBoMRzTHQox2yatX29+TlzXKVOTyDALTmo2vK9ZrnEuzAFZT2+5TKeAwPWVyHqWgzQ3HZQa/qubC+w1Tic5n9ESXdRk4GFs7EZetXzn7HKfMDGyG2YfWyxaIhNmlCf2XxCVSdTLYtvwmIBwKBgFId+Bdw08G3K3K0yunHVranXWc641YS0l21VmlPqmlihqiRmgaVr/umvysfGQErZYPG9h0KIUgFX5SDvqDDbUj38fPPPV5IN7+XR/hhv5ba8Qt42jMOHmfDVTbW9vkOqCYN/pgHFkxR+nLKF2i/lu/8QKnZTsfSMp7hbMXvaU3ZAoGARyQAW7o0OyBFB8PbD9WTzyDhIdScnLbtIpDKSDxwmFM+okwaRSokKDOBZoGFMhp/4kwQdYkkkQ6BGPB84qMcENoSLaa/quxG+dIYfMzLlGnldoDVlW/ylyY32o+27fvX6reLExqyUD1bQtGM2iyDJLnePswXdani9ddPhFJDWiMCgYEA4hTjOX72ZvDmqLaXfd7/HbbpN9m/3Oaf9mwT6D4JU3DyRmPCQbmx47YR4VC4Y9WR+922y/biAGYQ5SFuxkphvLq3VwTxUdioNHIdWcT9O957SENYKSunrVjO+d0hlv6eUZAL0Xapl9CbWOsPIAECPnFkwLS/utT+I1/P7fIPMTk=";

    // 服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public static String notify_url ="http://order.skd.com/payed/notify";
    // 页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问 商户可以自定义同步跳转地址
    public static String return_url = "http://order.skd.com/orderPay/returnUrl";
    // 请求网关地址
    public static String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    // 编码
    public static String CHARSET = "UTF-8";
    // 返回格式
    public static String FORMAT = "json";
    // 支付宝公钥
    public static String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArRcQoiAjZdzPPpfy5cY+UbwzLqlLb+N1ESSmN1ncCyS2L1Jk/6wcrlFghARL0pFnkLfgiY1/WuW5vdugHad42S23aBe6Z5lhE88TF0g405HYAYy/nY+5tAGhRVLuY//RKysC99JOO/cYF0UdkQhzW5jH7OBoVlylaljFrnT/zW4BjZ8BkatXw4C05KCOUsDMQHWHdQ7MtFYd9IvoVQ19g4WgCJwMSNKNJhPFIoejt3QvaSR2Cn10i1QxoeN4H5qa+FiC7L5mdiaJZbw8FZ6VECQCW5e2SoN4k5UZaAgJPxSSRFRcicv5l06sNwGqV5oCOTzcODA5JnzMGaEbsmBZoQIDAQAB";
    // 日志记录目录
    public static String log_path = "/log";
    // RSA2
    public static String SIGNTYPE = "RSA2";

    public String pay(PayVo payVo){
        // SDK 公共请求类，包含公共请求参数，以及封装了签名与验签，开发者无需关注签名与验签
        //调用RSA签名方式
        AlipayClient client = new DefaultAlipayClient(URL,
                APPID,
                RSA_PRIVATE_KEY,
                FORMAT,
                CHARSET,
                ALIPAY_PUBLIC_KEY,
                SIGNTYPE);
        AlipayTradeWapPayRequest alipay_request=new AlipayTradeWapPayRequest();

        // 封装请求支付信息
        AlipayTradeWapPayModel model=new AlipayTradeWapPayModel();
        model.setOutTradeNo(payVo.getOut_trader_no());
        model.setSubject(payVo.getSubject());
        model.setTotalAmount(payVo.getTotal_amount());
        model.setBody(payVo.getBody());
        model.setTimeoutExpress("5000");
        model.setProductCode("11111");
        alipay_request.setBizModel(model);
        // 设置异步通知地址
        alipay_request.setNotifyUrl(notify_url);
        // 设置同步地址
        alipay_request.setReturnUrl(return_url);
        // form表单生产
        String form = "";
        try {
            // 调用SDK生成表单
            form = client.pageExecute(alipay_request).getBody();
            return form;
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  null;
    }
}
